package com.dp.appi.controller;

import com.dp.appi.entity.UserEntity;
import com.dp.appi.exception.CommonException;
import com.dp.appi.service.ConsumerService;
import com.dp.appi.service.StageService;
import com.dp.appi.util.ExceptionUtil;
import com.dp.appi.util.SYS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

/**
 * Created by bchen on 2016/5/30.
 */
@Controller
@RequestMapping(value = "/stageControl")
public class StageControl {
    private static Logger log = LoggerFactory.getLogger(StageControl.class);
    @Autowired
    private StageService stageService;

    @Autowired
    private ConsumerService consumerService;

    /**
     * 阶段交叉跨越
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/getNewNum", method = RequestMethod.GET)
    public void getNewNum(ModelMap modelMap, @RequestParam Map<String, String> param, Long stage_id, Integer pt_id) {
        try {
            modelMap.addAttribute("newNum", stageService.newExpNum(stage_id, pt_id));
        } catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 检测用户上传权限
     */
    @RequestMapping(value = "/checkStage", method = RequestMethod.GET)
    public void checkStage(ModelMap modelMap, String stageId, @RequestParam Map<String, String> param) {
        //检查阶段进度
        //如果是踏勘都可以
        //如果是设计，当前用户只具备踏勘权限，这不通过
        //如果是审核和交互都不能上传
        if (stageId != null) {
            Map<String, Object> stageInfo = stageService.getStageInfo(stageId);
            if (stageInfo != null) {
                Boolean isDeleted = Boolean.parseBoolean(String.valueOf(stageInfo.get("deleted")));
                if (isDeleted) {//项目是否删除
                    modelMap.addAllAttributes(SYS.PACK0010.msgMap());
                } else {
                    //判断项目归属是否等于人员组织机构
//                    if (isLong(stageId)) {
                        Map<String, Object> proMap = stageService.getProjectInfo(Long.parseLong(String.valueOf(stageInfo.get("project_id"))));
                        Object cost_center = proMap.get("cost_center");
                        if (!StringUtils.isEmpty(cost_center)) {
                            int current_schedule = Integer.parseInt(String.valueOf(stageInfo.get("current_schedule")));
                            String openIdent = param.get("openIdent");
                            UserEntity userEntity = UserEntity.decrypt(openIdent);
                            Map<String, Object> map = consumerService.getUser(userEntity);
                            String userId = String.valueOf(map.get("id"));
                            //判断项目归属是否等于人员组织机构
                            Map<String, Object> partMap = consumerService.getTSpartByUserId(userId);
                            String org_id = "";
                            if (partMap != null) {
                                org_id = String.valueOf(partMap.get("org_id"));
                                //项目归属等于人员组织机构
                                if (cost_center.equals(org_id)) {
                                    if (current_schedule == 11) {//踏勘
                                        modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
                                    } else if (current_schedule == 12) {//设计

                                        modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
//                                        String designer = String.valueOf(stageInfo.get("designer"));//设计人员
////                                    String explorationer=String.valueOf(stageInfo.get("explorationer"));//踏勘人
//                                        String manager = String.valueOf(stageInfo.get("manager"));//负责人
//                                        String auditor = String.valueOf(stageInfo.get("auditor"));//审核人
//                                    boolean a = manager.indexOf(userId) != -1;
//                                    boolean b = designer.indexOf(userId)==-1;
//                                    boolean c = auditor.indexOf(userId)!=-1;
//                                        if (manager.indexOf(userId) != -1 || designer.indexOf(userId) != -1 || auditor.indexOf(userId) != -1) {
//                                            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
//                                        } else {
//                                            modelMap.addAllAttributes(SYS.PACK0008.msgMap());
//                                        }
                                    } else if (current_schedule == 13) {//审核
                                       // modelMap.addAllAttributes(SYS.PACK0006.msgMap());
                                        modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
                                    } else if (current_schedule == 14) {//交付
                                       // modelMap.addAllAttributes(SYS.PACK0007.msgMap());
                                        modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
                                    } else {
                                        modelMap.addAllAttributes(SYS.E10002.msgMap());
                                    }
                                } else {//不等的时候
                                    modelMap.addAllAttributes(SYS.PACK0013.msgMap());
                                }
                            }
                        } else {
                            modelMap.addAllAttributes(SYS.PACK0011.msgMap());
                        }
//                    }
                }

            } else {
                modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
            }

        } else {
            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
        }

    }


    /**
     * 检测用户上传 随手拍权限
     */
    @RequestMapping(value = "/checkStagePh", method = RequestMethod.GET)
    public void checkStagePh(ModelMap modelMap, String stageId, @RequestParam Map<String, String> param) {
        //检查阶段进度
        //如果是踏勘都可以
        //如果是设计，当前用户只具备踏勘权限，这不通过
        //如果是审核和交互都不能上传
        if (stageId != null) {
            Map<String, Object> stageInfo = stageService.getStageInfo(stageId);
            if (stageInfo != null) {
                Boolean isDeleted = Boolean.parseBoolean(String.valueOf(stageInfo.get("deleted")));
                if (isDeleted) {//项目是否删除
                    modelMap.addAllAttributes(SYS.PACK0010.msgMap());
                } else {
                    //判断项目归属是否等于人员组织机构
//                    if (isLong(stageId)) {
                    Map<String, Object> proMap = stageService.getProjectInfo(Long.parseLong(String.valueOf(stageInfo.get("project_id"))));
                    Object cost_center = proMap.get("cost_center");
                    if (!StringUtils.isEmpty(cost_center)) {
                        int current_schedule = Integer.parseInt(String.valueOf(stageInfo.get("current_schedule")));
                        String openIdent = param.get("openIdent");
                        UserEntity userEntity = UserEntity.decrypt(openIdent);
                        Map<String, Object> map = consumerService.getUser(userEntity);
                        String userId = String.valueOf(map.get("id"));
                        //判断项目归属是否等于人员组织机构
                        Map<String, Object> partMap = consumerService.getTSpartByUserId(userId);
                        String org_id = "";
                        if (partMap != null) {
                            org_id = String.valueOf(partMap.get("org_id"));
                            //项目归属等于人员组织机构
                            if (cost_center.equals(org_id)) {
                                if (current_schedule == 11) {//踏勘
                                    modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
                                } else if (current_schedule == 12) {//设计

                                    modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
//                                        String designer = String.valueOf(stageInfo.get("designer"));//设计人员
////                                    String explorationer=String.valueOf(stageInfo.get("explorationer"));//踏勘人
//                                        String manager = String.valueOf(stageInfo.get("manager"));//负责人
//                                        String auditor = String.valueOf(stageInfo.get("auditor"));//审核人
//                                    boolean a = manager.indexOf(userId) != -1;
//                                    boolean b = designer.indexOf(userId)==-1;
//                                    boolean c = auditor.indexOf(userId)!=-1;
//                                        if (manager.indexOf(userId) != -1 || designer.indexOf(userId) != -1 || auditor.indexOf(userId) != -1) {
//                                            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
//                                        } else {
//                                            modelMap.addAllAttributes(SYS.PACK0008.msgMap());
//                                        }
                                } else if (current_schedule == 13) {//审核
                                    // modelMap.addAllAttributes(SYS.PACK0006.msgMap());
                                    modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
                                } else if (current_schedule == 14) {//交付
                                    // modelMap.addAllAttributes(SYS.PACK0007.msgMap());
                                    modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
                                } else {
                                    modelMap.addAllAttributes(SYS.E10002.msgMap());
                                }
                            } else {//不等的时候
                                modelMap.addAllAttributes(SYS.PACK0013.msgMap());
                            }
                        }
                    } else {
                        modelMap.addAllAttributes(SYS.PACK0011.msgMap());
                    }
//                    }
                }

            } else {
                modelMap.addAllAttributes(SYS.E10003.msgMap());
            }

        } else {
            modelMap.addAllAttributes(SYS.E10003.msgMap());
        }

    }
    public static boolean isLong(String value) {
        try {
            Long.parseLong(value);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
}
